﻿Imports System
Imports CustomerBrowser.ControlValidator
Public Class CustomerValidationRules
    Implements IRulesList

    Public Shared ReadOnly Property Instance() As CustomerValidationRules
        Get
            Return New CustomerValidationRules
        End Get
    End Property

    Public ReadOnly Property Rules() As Predicate(Of IRulesList.RuleParams)() _
                                                        Implements IRulesList.Rules
        Get
            Return New Predicate(Of IRulesList.RuleParams)() { _
                                                AddressOf TerritoryId, _
                                                AddressOf FirstName}
        End Get
    End Property

    <Validation("TerritoryID", "TerritoryID must be >0")> _
        Public Function TerritoryId(ByVal data As IRulesList.RuleParams) As Boolean
        Try
            If Not TypeOf (data.NewData) Is Integer Then Return False
            Dim newVal As Integer = CInt(data.NewData)
            If newVal > 0 Then Return True
            Return False
        Catch ex As Exception
            Return False
        End Try
    End Function

    <Validation("FirstName", "First Name must be specified")> _
    Public Function FirstName(ByVal data As IRulesList.RuleParams) As Boolean
        Try
            Dim newVal As String = TryCast(data.NewData, String)
            If newVal = "" Then Return False
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function
End Class
